package com.csoa.community.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.collect.community.R;
import com.csoa.community.activity.WZInfoActivity;
import com.csoa.community.activity.WZInfoTableActivity;
import com.csoa.community.bean.WZInfoBean;
import com.csoa.community.db.WZInfoDB;
import com.csoa.community.widget.CommonDialog;
import com.csoa.community.widget.CommonDialog.OnListItemClickListener;

public class WZTableAdapter extends BaseAdapter {

	private WZInfoTableActivity context;
	private LayoutInflater mInflater;
	private List<WZInfoBean> mItemList;

	public WZTableAdapter(WZInfoTableActivity context, List<WZInfoBean> lists) {
		super();
		this.context = context;
		this.mItemList = lists;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mItemList == null ? 0 : mItemList.size();
	}

	@Override
	public WZInfoBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return mItemList == null ? null : mItemList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int index, View view, ViewGroup arg2) {
		View tempItemView = null;
		ViewHolder viewHolder = null;

		if (view == null) {
			tempItemView = mInflater.inflate(R.layout.list_item_wz, null);
			viewHolder = new ViewHolder();
			viewHolder.latinName = (TextView) tempItemView
					.findViewById(R.id.tvLatinName);
			viewHolder.chineseName = (TextView) tempItemView
					.findViewById(R.id.tvChineseName);
			viewHolder.commonName = (TextView) tempItemView
					.findViewById(R.id.tvCommonName);
			viewHolder.phenology = (TextView) tempItemView.findViewById(R.id.tvPhenology);
			viewHolder.niche = (TextView) tempItemView.findViewById(R.id.tvNiche);
			viewHolder.level = (TextView) tempItemView.findViewById(R.id.tvLevel);
			tempItemView.setTag(viewHolder);
		} else {
			tempItemView = view;
			viewHolder = (ViewHolder) tempItemView.getTag();
		}

		if (mItemList.size() > 1) {
			if (index == 0) {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_top);
			} else if (index == mItemList.size() - 1) {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_bottom);
			} else {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_middle);
			}

		} else if (mItemList.size() == 1) {
			tempItemView
					.setBackgroundResource(R.drawable.background_view_rounded_single);
		}

		WZInfoBean wzInfo = getItem(index);
		viewHolder.latinName.setText(Html.fromHtml("<html><font color='red'>拉丁学名:</font></html>") + wzInfo.latinName);
		viewHolder.chineseName.setText("中文名:" + wzInfo.chineseName);
		viewHolder.commonName.setText("俗名:" +  wzInfo.commonName);
		viewHolder.phenology.setText("物候:" + wzInfo.phenology);
		viewHolder.niche.setText("生态位:" + wzInfo.niche);
		viewHolder.level.setText("层次:" + wzInfo.level);

		tempItemView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDefineDialog(index);
			}
		});

		return tempItemView;
	}

	private static final class ViewHolder {
		TextView latinName;
		TextView chineseName;
		TextView commonName;
		TextView phenology;
		TextView niche;
		TextView level;
	}

	private void showDefineDialog(final int position) {
		String items[] = new String[] { "查看物种", "添加物种", "删除物种", "修改物种"
				};

		CommonDialog caculatorDialog = new CommonDialog(context);
//		caculatorDialog.setDialogTitle("常用语");
		caculatorDialog.setItems(items, new OnListItemClickListener() {

			@Override
			public void onListItemClick(int which) {
				if (which == 0) {
					startWZInfoActivity(which, position);
				}
				if (which == 1) {
					Intent intent = new Intent(context, WZInfoActivity.class);
					intent.putExtra("yfId", mItemList.get(position).yfId);
					intent.putExtra("which", which);
					context.startActivityForResult(intent, 0);
				}
				// 删除
				if (which == 2) {
					if (position < mItemList.size()) {
						
						new AlertDialog.Builder(context)
						.setMessage("确认删除吗?")
						.setPositiveButton("确定", new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								WZInfoDB.delete(mItemList.get(position));
								mItemList.remove(position);
								WZTableAdapter.this.notifyDataSetChanged();
							}
						}).setNegativeButton("取消", null).show();
						
						
						
					}
				}
				if (which == 3) {
					startWZInfoActivity(which, position);
				}

			}
		});
		caculatorDialog.show();
	}

	
	public void startWZInfoActivity(int which,int position) {
		Intent intent = new Intent(context, WZInfoActivity.class);
		intent.putExtra("WZInfo", mItemList.get(position));
		intent.putExtra("position", position);
		intent.putExtra("yfId", mItemList.get(position).yfId);
		intent.putExtra("which", which);
		context.startActivityForResult(intent,0);
	}

}
